// 计算两个日期之间的相隔天数
export function getDaysApart (KSSJ, JSSJ) {
  // 两个日期
  const date1 = new Date(KSSJ).getTime() // 第一个日期
  const date2 = new Date(JSSJ).getTime() // 第二个日期
  // 计算相隔天数
  const timeDifference = date2 - date1
  // 将时间差转换为天数
  const daysDifference = Math.floor(timeDifference / (1000 * 60 * 60 * 24))
  return daysDifference
}
// 查找数据中最大的日期
export function getMaximumDate (data, key) {
  const maxDate = data.reduce((max, obj) =>
    (obj[key] > max ? obj[key] : max), data[0][key]
  )
  return maxDate
}
// 依据最大日期填充
export function getFillInDate (val) {
  const maxDate = new Date(val); // 假设最大日期为2022年4月5日
  const currentDate = new Date(); // 获取当前日期
  const currentYear = currentDate.getFullYear(); // 获取当前年份

  const startDate = new Date(currentYear, 0, 1); // 本年初的日期，即1月1日
  const endDate = maxDate; // 最大日期

  const dates = [];
  let currentDatePointer = startDate;

  while (currentDatePointer <= endDate) {
    const year = currentDatePointer.getFullYear();
    const month = String(currentDatePointer.getMonth() + 1).padStart(2, '0');
    const day = String(currentDatePointer.getDate()).padStart(2, '0');
    const formattedDate = `${year}-${month}-${day}`;
    dates.push(formattedDate);
    currentDatePointer = new Date(currentDatePointer.getTime() + 24 * 60 * 60 * 1000); // 增加一天
  }
  return dates
}

// 获取当前时间
export function getNowTime () {
  const now = new Date()
  const year = now.getFullYear()
  const month = String(now.getMonth() + 1).padStart(2, '0')
  const day = String(now.getDate()).padStart(2, '0')
  const hours = String(now.getHours()).padStart(2, '0')
  const minutes = String(now.getMinutes()).padStart(2, '0')
  const seconds = String(now.getSeconds()).padStart(2, '0')
  const currentTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
  return currentTime
}